package com.demo.datastructure.stack;

/**
 * @description:
 * @author: ljr
 * @time: 2021/4/27 15:10
 */
public class MyCounter{

    public int calculate(String s) {
       MyStack<Integer> intStack = new MyStack<>();
       MyStack<Character> charStack = new MyStack<>();

       String news = s.replaceAll(" ","");
       char[] chars = news.toCharArray();
       for(int i = 0; i < chars.length; i++){
            int charNum = 0;
            if((charNum = charset(chars[i])) == 0){
                int num = converInt(chars[i]);
                if(intStack.isEmpty()){
                    intStack.push(num);
                    continue;
                }


                if(charset(chars[i-1]) == 0){
                    intStack.push(intStack.pop()*10+num);
                    continue;
                }

                int towCharNum = charset(charStack.peek());
                if(towCharNum == 1 || towCharNum == 2){
                    charStack.pop();
                    if(!charStack.isEmpty()){
                        int towCharNum2 = charset(charStack.peek());
                        if(towCharNum2 == 1 || towCharNum2 == 2){
                            charStack.pop();
                            num = counter(intStack.pop(),num,towCharNum,towCharNum2);
                            intStack.push(num);
                            continue;
                        }
                    }

                    num = counter(intStack.pop(),num,towCharNum,0);
                    intStack.push(num);
                    continue;
                }
                intStack.push(num);
            }else{
                if(charNum == 4){
                    charStack.pop();
                    System.out.println("i==="+i);
                    peekCounter(charStack,intStack);
                }else{
                    charStack.push(chars[i]);
                }
            }
       }

       if(!intStack.isEmpty()){
           return intStack.pop();
       }

       return 0;
    }

    private int converInt(char a){
        return Integer.valueOf(a)-48;
    }

    private void peekCounter(MyStack<Character> charStack, MyStack<Integer> intStack) {
        if(charStack.isEmpty()){
            return;
        }

        int towCharNum = charset(charStack.peek());
        if(towCharNum != 1 && towCharNum != 2){
            return;
        }
        charStack.pop();
        int oneNum = intStack.pop();
        System.out.println(oneNum);
        int twoNum = intStack.pop();

        if(!charStack.isEmpty()){
            int towCharNum2 = charset(charStack.peek());
            if(towCharNum2 == 1 || towCharNum2 == 2){
                charStack.pop();
                int num = counter(twoNum,oneNum,towCharNum,towCharNum2);
                intStack.push(num);
                return;
            }
        }

        int num = counter(twoNum,oneNum,towCharNum,0);
        intStack.push(num);
    }

    private int counter(int a, int b, int charNum,int charNum2){
        int num = 0;
        if(charNum == 1){
            num = a+b;
        }else{
            num = a-b;
        }
        if(charNum2 == 2){
            num = num - a*2;
        }
        return num;
    }

    private int charset(Character character){
        switch (character){
            case '+' :
                return 1;
            case '-' :
                return 2;
            case '(' :
                return 3;
            case ')' :
                return 4;
            default:
                return 0;
        }

    }


    public static void main(String[] args) {
        MyCounter myCounter = new MyCounter();
        int num = myCounter.calculate("1-(4+5+2+(2+2))-3)+(6+8)");
        System.out.println(num);

    }
}
